import type { Resource, ResourceLanguage } from 'i18next'

type LanguageFile = {
  default: ResourceLanguage
}

const modules = import.meta.glob<true, string, LanguageFile>("./**/index.ts", { eager: true });

const getLangAll = () => {
  const message: Resource = {};
  getLangFiles(modules, message);
  return message;
}

const getLangFiles = (mList: Record<string, LanguageFile>, msg: Resource) => {
  for (const path in mList) {
    if (mList[path].default) {
      const pathName = path.split('/').at(-2)
      if (msg[pathName]) {
        msg[pathName] = {
          translation: {
          ...mList[pathName],
          ...mList[path].default
        }};
      } else {
        msg[pathName] = {translation: mList[path].default};
      }
    }
  }
}

const langData = getLangAll()

export default langData;
